package com.AAA.controller.fpa;

import com.AAA.Util.EasyExcelUtil;
import com.AAA.entity.LayData;
import com.AAA.entity.MyPage;
import com.AAA.entity.fpa.TFpa;
import com.AAA.service.Impl.fpa.ITFpaServiceImpl;
import com.AAA.service.fpa.ITFpaService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * (TFpa)表控制层
 *
 * @author 何泽明
 * @since 2022-11-05 12:08:35
 */
@Controller
@RequestMapping("tFpa")
public class TFpaController {
    /**
     * 服务对象
     */
    @Autowired
    private ITFpaService service;
    @Autowired
    private ITFpaServiceImpl serviceimpl;

    //批量添加
    @PostMapping("addAll")
    @ResponseBody
    public LayData LayData (HttpServletRequest request, Model model, MultipartFile file) throws IOException {
        List<Object> objects = EasyExcelUtil.readExcel(file, new TFpa(), 1, 1);
        int count=0;
        List<Integer> list=new ArrayList<>();
        LayData layData=new LayData();
        for (Object t:objects){
            TFpa tFpa= (TFpa) t;
            count++;
            Integer addall = serviceimpl.addAll(tFpa);
            if (addall!=addall){
                list.add(count);
            }
        }
        if (list.size()==0){
            layData.setMsg("添加成功");
        }else {
            layData.setData("有第"+list.toString()+"条未添加成功");
        }
        return layData;
    }

    @RequestMapping("queryAll")
    @ResponseBody
    public LayData queryall(MyPage myPage, TFpa tFpa){
        PageInfo pageInfo=service.query(myPage,tFpa);
        return new LayData(pageInfo);
    }

    @PostMapping("add")
    @ResponseBody
    public LayData add(TFpa tFpa){
        if(tFpa.getQid()==null){
            return new LayData(service.add(tFpa));
        }else{
            return new LayData(service.edit(tFpa));
        }
    }

    @PostMapping("del")
    @ResponseBody
    public LayData delete(Integer qid){
        return new LayData( service.delete(qid));
    }

    // 批量导入数据
    @PostMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {
        service.dealFile(file);
        return new LayData(true);
    }

    @GetMapping
    @ResponseBody
    public LayData getAllSub(MyPage myPage){
        return new LayData(service.getAll(myPage));
    }

    /**
     * fpa做题页面
     * @param myPage
     * @param request
     * @return
     */
    @GetMapping("index")
    public String getIndexAllSub(MyPage myPage, HttpServletRequest request){
        System.out.println("-----------------------------");
        myPage.setLimit(30);
        myPage.setPage(1);
        PageInfo all = service.getAll(myPage);
        request.setAttribute("allsub",all);
        return "/fpa/fpaindex";
    }
}

